﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Chat
{
    /// <summary>
    /// Implementation for chat commnds serailization, which uses the standard
    /// .NET binary serailization.
    /// </summary>
    /// <typeparam name="OutCmd">Base class for serialized (outcomming) chat commands.</typeparam>
    public class CmdBinSerializer<OutCmd> : CmdSerializer<OutCmd>
        where OutCmd : Command
    {
        protected BinaryFormatter InFormatter { get; set; }
        protected BinaryFormatter OutFormatter { get; set; }

        public CmdBinSerializer() : base()
        {
            InFormatter = new BinaryFormatter();
            OutFormatter = new BinaryFormatter();
        }

        public override void Serialize(Stream serializationStream, OutCmd command)
        {
            OutFormatter.Serialize(serializationStream, command);
        }

        public override object Deserialize(Stream serializationStream)
        {
            return InFormatter.Deserialize(serializationStream);
        }
    }
}
